package net.towee.client.com.lobby.presenter;

import net.towee.client.com.app.Presenter;
import net.towee.client.com.lobby.Lobby;
import net.towee.client.com.lobby.LobbyStep;
import net.towee.client.managers.AccountHelper;
import net.towee.client.managers.AccountsManager;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * Lobby step for changing user nickname.
 * 
 * @author c58
 */
@Singleton
public class LobbyNicknamePresenter implements Presenter, LobbyStep, ChangeHandler, ClickHandler {

	public interface Display extends IsWidget {
		HasChangeHandlers getNicknameChanger();

		HasClickHandlers getNextStepButton();
		
		HasClickHandlers getCloseWizardButton();

		String getEnteredNickname();
		
		void setNickname(String text);

		void nicknameNotUsed();

		void nicknameUsed();
		
		void nicknameUpdated();

		void invalidCharacter();
	}

	private final Display display;
	private final Lobby lobby;
	private final AccountsManager accountManager;
	private final AccountHelper accountHelper;
	
	private boolean validName = false;
	private boolean updated = false;

	@Inject
	public LobbyNicknamePresenter(Lobby lobby, AccountsManager accountManager,
			AccountHelper accountHelper, Display display) {
		this.display = display;
		this.lobby = lobby;
		this.accountManager = accountManager;
		this.accountHelper = accountHelper;
		
		// Set handler for buttons
		display.getCloseWizardButton().addClickHandler(this);
		display.getNextStepButton().addClickHandler(this);
		
		// Set handler for changing area
		display.getNicknameChanger().addChangeHandler(this);
	}

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add( display.asWidget() );
	}

	@Override
	public boolean validateStep() {
		GWT.log("validate account identity");
		if(validName && !updated) {
			accountHelper.updateAccountIdentity(new AsyncCallback<Boolean>(){
				@Override
				public void onFailure(Throwable caught) {
				}

				@Override
				public void onSuccess(Boolean result) {
					updated = true;
					
					display.setNickname( accountHelper.getAccount().getAccountIdentity().getNickName() );
					display.nicknameUpdated();
				}
			});
		}
		
		if(!validName)
			onChange(null);
			
		return validName;
	}

	@Override
	public void onClick(ClickEvent event) {
		Object source = event.getSource();
		
		if(source.equals( display.getNextStepButton() )) 
			lobby.showNextStep();
		else if(validateStep()) 
			lobby.closeLobby();
	}

	@Override
	public void onChange(ChangeEvent event) {
		validName = false;
		
		if(!accountHelper.getAccount().getAccountIdentity().setNickName( display.getEnteredNickname() ))
			display.invalidCharacter();
		else
			accountManager.checkAccountWithNicknameExists(display.getEnteredNickname(), new AsyncCallback<Boolean>(){
				@Override
				public void onFailure(Throwable caught) {}

				@Override
				public void onSuccess(Boolean result) {
					validName = !result;
					GWT.log("nickname checked: "+result);
				
					if(result == true) 
						display.nicknameUsed();
					else {
						display.nicknameNotUsed();
					}
				}
			});
	}
}